package com.yx.admin;

import java.io.File;
import java.io.FileOutputStream;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class StorageServerConfig extends Composite {
	private Text ID;
	private Text PORT;
	private Text IO_BUFFER;
	private Text THREAD;
	private Table backupservers;
	private Text MEN_MIN;
	private Text MEN_MAX;
	private Text BACKUP_ID;
	private Text BACKUP_URL;
	private Label message;
	private Text TCP_BUFFER;
	private Text XSS;
	private Text XMN;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public StorageServerConfig(Composite parent, int style) {
		super(parent, style);
		
		Label lblid = new Label(this, SWT.NONE);
		lblid.setBounds(23, 10, 59, 17);
		lblid.setText("节点名称：");
		
		ID = new Text(this, SWT.BORDER);
		ID.setBounds(85, 7, 61, 23);
		
		Label label = new Label(this, SWT.NONE);
		label.setBounds(211, 10, 37, 17);
		label.setText("端口：");
		
		PORT = new Text(this, SWT.BORDER);
		PORT.setText("10086");
		PORT.setBounds(250, 7, 61, 23);
		
		Label lblIo = new Label(this, SWT.NONE);
		lblIo.setBounds(192, 35, 56, 17);
		lblIo.setText("I/O缓存：");
		
		IO_BUFFER = new Text(this, SWT.BORDER);
		IO_BUFFER.setText("512");
		IO_BUFFER.setBounds(250, 32, 61, 23);
		
		Label label_1 = new Label(this, SWT.NONE);
		label_1.setBounds(348, 10, 59, 17);
		label_1.setText("并发线程：");
		
		THREAD = new Text(this, SWT.BORDER);
		THREAD.setText("20");
		THREAD.setBounds(412, 7, 61, 23);
		
		backupservers = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
		backupservers.setBounds(10, 124, 475, 132);
		backupservers.setHeaderVisible(true);
		backupservers.setLinesVisible(true);
		
		Label lblK = new Label(this, SWT.NONE);
		lblK.setBounds(312, 35, 17, 17);
		lblK.setText("K");
		
		Label label_2 = new Label(this, SWT.NONE);
		label_2.setText("初始堆内存：");
		label_2.setBounds(336, 35, 72, 17);
		
		MEN_MIN = new Text(this, SWT.BORDER);
		MEN_MIN.setText("2048");
		MEN_MIN.setBounds(411, 32, 61, 23);
		
		Label label_3 = new Label(this, SWT.NONE);
		label_3.setText("最大堆内存：");
		label_3.setBounds(10, 60, 72, 17);
		
		MEN_MAX = new Text(this, SWT.BORDER);
		MEN_MAX.setText("2048");
		MEN_MAX.setBounds(85, 57, 61, 23);
		
		Label lblM = new Label(this, SWT.NONE);
		lblM.setText("M");
		lblM.setBounds(147, 60, 17, 17);
		
		Label label_4 = new Label(this, SWT.NONE);
		label_4.setText("M");
		label_4.setBounds(475, 35, 17, 17);
		
		Label label_5 = new Label(this, SWT.NONE);
		label_5.setBounds(10, 96, 96, 17);
		label_5.setText("备份服务器设置：");
		
		Button button = new Button(this, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String backId=BACKUP_ID.getText().trim();
				String backUrl=BACKUP_URL.getText().trim();
				if(backId==null||backId.length()!=5){
					BACKUP_ID.setText("");
					BACKUP_ID.setFocus();
					message.setText("输入错误，输入定长5位的唯一编号！");
					return ;
				}
				if(backUrl==null||backUrl.length()<1||backUrl.indexOf(":")<=0){
					message.setText("输入错误，地址格式为 url:port！");
					return ;
				}
				for(int i=0;i<backupservers.getItemCount();i++){
					TableItem item=backupservers.getItem(i);
					if(backId.equals(item.getText(0))){
						message.setText("输入错误，表中存在同样节点名称的备份服务器！");
						return ;
					}
					if(backUrl.equals(item.getText(1))){
						message.setText("输入错误，表中存在同样地址和端口的备份服务器！");
						return ;
					}
					
				}
				TableItem item = new TableItem(backupservers, SWT.NONE);
				item.setText(new String[]{backId,backUrl});
			}
		});
		button.setBounds(381, 91, 49, 27);
		button.setText("添加");
		
		Button button_1 = new Button(this, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int selectIdx=backupservers.getSelectionIndex();
				if(backupservers.getItemCount()<1)return;
				if(selectIdx<0||selectIdx>=backupservers.getItemCount()){
					return ;
				}
				backupservers.remove(selectIdx);				
			}
		});
		button_1.setText("删除");
		button_1.setBounds(436, 91, 49, 27);
		
		Button button_2 = new Button(this, SWT.NONE);
		button_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String id=ID.getText().trim();
				if(id==null||id.length()!=5){
					ID.setText("");
					ID.setFocus();
					message.setText("输入错误，输入定长5位的唯一编号！");
					return ;
				}
				try{
					int port=getInt(PORT);
					int io_buffer=getInt(IO_BUFFER);
					int tcp_buffer=getInt(TCP_BUFFER);
					int thread=getInt(THREAD);
					int men_min=getInt(MEN_MIN);
					int men_max=getInt(MEN_MAX);
					int xmn=getInt(XMN);
					int xss=getInt(XSS);
					if(backupservers.getItemCount()<1){
						message.setText("设置错误，至少设置一台备份服务器！");
						return;
					}
					String command="java -server -Xms"+men_min+"m -Xmx"+men_max+"m -Xmn"+xmn+"m -Xss"+xss+"k -Dfile.encoding=\"UTF-8\" -cp ServiceBuilder.jar com.yx.filesystem.storage.Server ";
					command=command+"id="+id+" port="+port+" TCPBuffer="+tcp_buffer+" IOBuffer="+io_buffer+" thread="+thread;
					for(int i=0;i<backupservers.getItemCount();i++){
						TableItem item=backupservers.getItem(i);
						command=command+" backup="+item.getText(0)+":"+item.getText(1);
					}
					File file=new File("./StorageStart.bat");
					if(file.exists())file.delete();
					FileOutputStream out=new  FileOutputStream(file,false);
					out.write(command.getBytes());
					out.flush();
					out.close();
					message.setText("启动文件生成成功，文件名：StorageStart.bat！");
				}catch(Exception ex){
					return;
				}
			}
		});
		button_2.setBounds(381, 281, 104, 27);
		button_2.setText("生成启动文件");
		
		Label label_6 = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_6.setBounds(10, 84, 475, 5);
		
		BACKUP_ID = new Text(this, SWT.BORDER);
		BACKUP_ID.setBounds(106, 93, 61, 23);
		
		BACKUP_URL = new Text(this, SWT.BORDER);
		BACKUP_URL.setBounds(173, 93, 202, 23);
		TableColumn tc1 = new TableColumn(backupservers, SWT.CENTER);
		TableColumn tc2 = new TableColumn(backupservers, SWT.CENTER);
		tc1.setText("节点名称");
		tc2.setText("节点地址");
		tc1.setWidth(100);
		tc2.setWidth(345);
		
		message = new Label(this, SWT.NONE);
		message.setBounds(10, 262, 475, 17);
		
		Label lblTcp = new Label(this, SWT.NONE);
		lblTcp.setText("TCP缓存：");
		lblTcp.setBounds(24, 35, 59, 17);
		
		TCP_BUFFER = new Text(this, SWT.BORDER);
		TCP_BUFFER.setText("512");
		TCP_BUFFER.setBounds(85, 32, 61, 23);
		
		Label label_7 = new Label(this, SWT.NONE);
		label_7.setText("K");
		label_7.setBounds(147, 35, 17, 17);
		
		Label label_8 = new Label(this, SWT.NONE);
		label_8.setText("栈内存：");
		label_8.setBounds(360, 60, 49, 17);
		
		XSS = new Text(this, SWT.BORDER);
		XSS.setText("1024");
		XSS.setBounds(411, 57, 61, 23);
		
		Label label_9 = new Label(this, SWT.NONE);
		label_9.setText("K");
		label_9.setBounds(475, 61, 17, 17);
		
		Label label_10 = new Label(this, SWT.NONE);
		label_10.setText("年轻代内存：");
		label_10.setBounds(175, 60, 72, 17);
		
		XMN = new Text(this, SWT.BORDER);
		XMN.setText("768");
		XMN.setBounds(250, 57, 61, 23);
		
		Label label_11 = new Label(this, SWT.NONE);
		label_11.setText("M");
		label_11.setBounds(312, 60, 17, 17);
	}
	private int getInt(Text com)throws Exception{
		String vStr=com.getText().trim();
		try{
			return Integer.parseInt(vStr);
		}catch(Exception e){
			com.setText("");
			com.setFocus();
			message.setText("输入错误，输入整数数字！");
			throw e;
		}
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
